package com.ophiux.FeverManagement.aplication.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByPatientRequest;
import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByPatientResponse;
import com.ophiux.FeverManagement.aplication.service.IGetPatientTreatmentListByPatientAppPort;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatmentVisit;
import com.ophiux.FeverManagement.domain.repository.IDoctorRepositories;
import com.ophiux.FeverManagement.domain.repository.IPatientTreatmentCurrentRepository;
import com.ophiux.FeverManagement.domain.repository.IPatientTreatmentRepository;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.exception.OphiuxRepositoryException;
import com.ophiux.utils.parser.AssemblyFactory;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class GetPatientTreatmentListByPatientAppService implements IGetPatientTreatmentListByPatientAppPort{
	
	
	@Autowired
	IDoctorRepositories doctorRepositories;
	@Autowired
	IPatientTreatmentRepository patientTreatmentRepository;
	@Autowired
	IPatientTreatmentCurrentRepository patientTreatmentCurrentRepository;
	
	@Override
	@Transactional(rollbackOn=OphiuxRepositoryException.class)
	public Result<List<getPatientTreatmentListByPatientResponse>> execute(getPatientTreatmentListByPatientRequest request) {
		try {
			if (!request.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail);
			}
			
			Map<String,String> map = new HashMap<String,String>();
			map.put("PATIENT_NAME", request.getPATIENT_NAME());
			map.put("TEL", request.getTEL());
			map.put("ID_NO", request.getID_NO());
			map.put("START_DATE", request.getSTART_DATE());
			map.put("END_DATE", request.getEND_DATE());
			List<RbPatientTreatmentVisit> list = patientTreatmentRepository.getAllDetaulVisit(map);
			
			if(list==null||list.size()<=0) {
				return Result.success();
			}
			
//			if(list!=null&&list.size()>0) {
//				//保存查询记录
//				RbPatientTreatmentCurrent model = new RbPatientTreatmentCurrent("",request.getPATIENT_NAME(),request.getTEL(),request.getID_NO());
//				patientTreatmentCurrentRepository.save(model);
//			}
			List<getPatientTreatmentListByPatientResponse> resp = AssemblyFactory.instance().parse(list, getPatientTreatmentListByPatientResponse.class);
			return Result.success(resp);
		} catch (Exception e) {
			log.info("GetPatientTreatmentListByPatientAppService=》execute(),程序异常：", e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
	}

}
